import request from './request';

const { protocol } = document.location;
const baseURL = import.meta.env.VITE_API_BASE_URL;
const basePATH = import.meta.env.VITE_API_BASE_PATH;
const url = '/adminapi/common/';

/**
 * 验证码
 */
export function apiCaptcha(params: any) {
  return request({
    url: `${url}captcha`,
    method: 'get',
    params,
  });
}

/**
 * 文件上传APi
 */
export function uploadURL() {
  return `${protocol}${baseURL}${url}upload`;
}

/**
 * 文件下载地址
 */
export function downloadPath() {
  return `${protocol}${basePATH}uploads/`;
}

/**
 * 文件上传
 */
export function apiUpload(
  data: FormData,
  config?: {
    controller: AbortController;
    onUploadProgress?: (progressEvent: any) => void;
  }
) {
  // const controller = new AbortController();
  // return request.post(`${url}upload`, data, config);
  return request({
    url: `${url}upload`,
    method: 'post',
    data,
    config,
  });
}

/**
 * 文件下载
 */
export function apiDownload(data: any) {
  return request({
    url: `${url}download`,
    method: 'post',
    data,
  });
}

/**
 * 设置
 */
export function apiSetting(data?: any, method = 'get') {
  // if (method === 'get') {
  //   return request.get(`${url}setting`, { data });
  // }
  // return request.post(`${url}setting`, data);
  return request({
    url: `${url}setting`,
    method,
    data,
  });
}

/**
 * 清除缓存
 */
export function apiClearCache(params?: any) {
  return request({
    url: `${url}download`,
    method: 'get',
    params,
  });
}

/**
 * 省市区
 */
export function apiRegions(params?: any) {
  return request({
    url: `${url}download`,
    method: 'get',
    params,
  });
}